package com.itheima.case2.web;

import com.itheima.case2.pojo.vo.*;
import com.itheima.case2.service.UserService;
import com.itheima.case2.utils.BaseController;
import com.itheima.case2.utils.BeansFactory;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/user/*")
public class UserServlet extends BaseServlet{
    public void add(HttpServletRequest request, HttpServletResponse response){
        try {
            //1.获取前端的请求数据封装到AddUser实体类中
            AddUser user = BaseController.parseJSON2Object(request, AddUser.class);
            //2.创建实现类对象
            UserService userService = BeansFactory.getBean("userService");
            //3.调用实现类中的方法
            userService.addUser(user);
            //4.将添加的用户结果返回
            Result result = new Result(true, "添加用户成功");
            BaseController.printResult(response,result);
        } catch (Exception e) {
            e.printStackTrace();
            //添加用户失败
            Result result = new Result(false, "添加用户失败");
            try {
                BaseController.printResult(response,result);
            } catch (Exception ioException) {
                ioException.printStackTrace();
            }
        }
    }
    public void delete(HttpServletRequest request, HttpServletResponse response){
        try {
            //接收请求的参数
            String uid = request.getParameter("uid");
            //1.创建实现类
           UserService userService = BeansFactory.getBean("userService");
            //2.调用实现类中删除用户的方法
            userService.deleteById(uid);
            //3.将结果返回
            Result result = new Result(true, "删除成功");
            BaseController.printResult(response,result);
        } catch (Exception e) {
            e.printStackTrace();
            Result result = new Result(false, "删除用户失败");
            try {
                BaseController.printResult(response,result);
            } catch (Exception ioException) {
                ioException.printStackTrace();
            }
        }
    }
    public void update(HttpServletRequest request, HttpServletResponse response){
        try {
            //1.将前端请求的数据封装到UpdateUser实体类中
            UpdateUser user = BaseController.parseJSON2Object(request, UpdateUser.class);
            //2.创建实现类
            UserService userService = BeansFactory.getBean("userService");
            //3.调用实现类中的方法
            userService.updateUser(user);
            //4.将查询的结果返回
            Result result = new Result(true, "修改用户成功");
            BaseController.printResult(response,result);
        } catch (Exception e) {
            e.printStackTrace();
            //5.查询失败
            Result result = new Result(false, "查询用户失败");
            try {
                BaseController.printResult(response,result);
            } catch (Exception ioException) {
                ioException.printStackTrace();
            }
        }
    }
    public void query(HttpServletRequest request, HttpServletResponse response){
        try {
            //1.将前端查询的数据封装到QueryPageBean实体类中
            QueryPageBean pb = BaseController.parseJSON2Object(request, QueryPageBean.class);
            //2.创建实现类
            UserService userService = BeansFactory.getBean("userService");
            //3.调用实现类中的查询方法
            PageResult userList= userService.queryUser(pb);
            //4.将查询的结果返回
            Result result = new Result(true, "查询用户成功", userList);
            BaseController.printResult(response,result);
        } catch (Exception e) {
            e.printStackTrace();
            //查询失败
            Result result = new Result(false, "查询用户失败");
            try {
                BaseController.printResult(response,result);
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        }
    }
}
